package com.dangf.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dangf.entity.User;
import com.dangf.service.UserService;

/**
 * 注册接口
 * @author hais1992
 * @date 2015年5月14日
 */
@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {
	@Resource
	private UserService userService;
	
	@RequestMapping("")
	@ResponseBody
	public Map<String, Object> index(String username,String password) {

		if (username == null || password == null) {
			return setResult(HTTP_CODE_FAILED, "注册失败，请输入账号密码！", "");
		}

		User u = userService.getUserByUsername(username);
		if (u != null) {
			return setResult(HTTP_CODE_FAILED, "注册失败，账号已存在！", "");
		}

		//开始注册
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		long id = (Long) userService.save(user);
		if(id!=0){
			u = userService.getEntityById(id);
			u.setPassword("******");
			return setResult(HTTP_CODE_SUCCESS, "注册成功！", u);
		}else {
			return setResult(HTTP_CODE_SERVER_ERROR, "注册失败，系统错误！", "");
		}
	}

}
